<?php

namespace App\Http\Controllers\Admin;

use App\Models\Orm\AwAuditRecord;
use App\Models\Orm\CooperativeCompany;
use App\Models\Orm\Deliver;
use App\Models\Orm\Product;
use App\Models\Orm\ProductAudit;
use App\Models\Orm\ProductLabels;
use App\Models\Orm\ProjectProduct;
use App\Models\Orm\Recommend;
use App\Models\Orm\SecondsKill;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

/**
 * 产品模块控制器
 *
 * Class ProductController
 * @package App\Http\Controllers\Admin
 */
class DeliverController extends Controller
{
    /**
     * 产品列表(展示页)
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user = auth('admin')->user();
        
        $data = Deliver::orderby('id', SORT_DESC)->get();
        return view('admin.deliver.index', compact('data'));

    }

    /**
     * 新建产品审核
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $user = auth('admin')->user();

        if ($request->isMethod('post')) {

            $request->flash();



            $data = $request ->all();
            $insert_data = $request->except(['_token','s']);


            // 用户ID
            $insert_data['admin_user_id'] = $user->id;
            $insert_data['created_at'] = date('Y-m-d H:i:s');
            $insert_data['updated_at'] = date('Y-m-d H:i:s');


            $insert_data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s');
//            $insert_data['company_id'] = $request->input('company_id');
//            $company = CooperativeCompany::find($data['company_id']);
//            $insert_data['admin_user_id'] = $company ? $company->user_id : 0;

            Deliver::create($insert_data);
            return redirect('/admin/deliver/index')->with(['success' => '您的申请已提交,请耐心等等待审核']);
        }

        $data = new Deliver();
        return view('admin.deliver.create', compact('data'));
    }



    /**
     * 修改产品详情审核
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request, $id)
    {
        // 当前用户
        $user = auth('admin')->user();

        // 产品信息
        $product = Deliver::find($id);

        if (!$product) {
            return back()->withErrors('未找到产品');
        }

        // post请求、提交表单
        if ($request->isMethod('post')) {

            $request->flash();

            $update_data = $request->except(['_token','s']);

            // 修改时间
            $update_data['updated_at'] = date('Y-m-d H:i:s');


//            $update_data['reg_cut']    = floatval($request->input('reg_cut'));
//            $company_id = intval($request->input('company_id'));
//
//            $company = CooperativeCompany::find($company_id);
//            $update_data['admin_user_id'] = $company ? $company->user_id : 0;
            // 更改产品表
            $product->update($update_data);

            return redirect('/admin/deliver/index')->with(['success' => '修改成功']);
        }

        $data = $product;



        return view('admin.deliver.edit', compact('data'));
    }


    /**
     * 产品上线
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function online($id)
    {


        // 当前用户
        $user = auth('admin')->user();

        // 产品信息
        $product = Deliver::find($id);


        if (!$product) {
            return back()->withErrors('未找到产品');
        }

        if ($product->status != Product::STATUS_OFF) {
            return back()->withErrors('该产品已经上线');
        }
        $product ->status = 1;

        $product ->save();

        return back()->with(['success' => '您的上线申请已提交,请耐心等待审核结果']);
    }

    /**
     * 产品下线
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function offline($id)
    {
        /**
         * 清除分类主题产品对照表关联数据
         * 清除产品标签表关联数据
         * 清除热门推荐表关联数据
         * 清除秒杀表关联数据
         */
        // 当前用户
        $user = auth('admin')->user();

        // 产品
        $product = Deliver::find($id);

        if (!$product) {
            return back()->withErrors('未找到相关产品');
        }


        if ($product->status != Product::STATUS_ON) {
            return back()->withErrors('该产品未上线');
        }
        $product ->status = 0;

        $product ->save();
        return back()->with(['success' => '产品下线成功']);
    }


}
